﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace Weazel.Willows.InterfaceGeneration
{
	public class IpifTopLevelEntity
	{
		private MemoryMap memoryMap;

		private string readImplementationFileSkeleton()
		{
			System.Reflection.Assembly assembly =
				System.Reflection.Assembly.GetExecutingAssembly();

			System.IO.Stream stream =
				assembly.GetManifestResourceStream("Weazel.Willows.InterfaceGeneration.ImplementationSkeleton.vhd");

			System.IO.StreamReader reader =
				new System.IO.StreamReader(stream);

			string result = reader.ReadToEnd();

			reader.Close();
			stream.Close();

			return string.Format(result, memoryMap.AddressesNeeded.ToString());
		}

		public IpifTopLevelEntity(MemoryMap memoryMap)
		{
			this.memoryMap = memoryMap;
		}

		public void WriteToFile(string filename)
		{
			string hej = readImplementationFileSkeleton();

			FileStream stream = null;
			StreamWriter writer = null;

			try
			{
				stream = File.Create(filename);
				writer = new StreamWriter(stream);
				writer.Write(hej);
			}
			finally
			{
				if (writer != null)
					writer.Close();
				if (stream != null)
					stream.Close();
			}
		}
	}
}
